const { UsersModel } =  require('../utils/db')

// 注册用户
const signup = (user) => {
  const userModel = new UsersModel(user)
  return userModel.save()
}

// 查询单个用户
const find = (username) => {
  return UsersModel.findOne({username})
}

// 查询多个用户
const list = () => {
  return UsersModel.find({})
}

// 修改用户名
const update = ({id, username}) => {
  return UsersModel.updateOne({_id: id}, {username})
  // return UsersModel.findByIdAndUpdate('5f9a81a769545b9f6dbf3b26', {$set: {username: 'aaa'}})
}

// 删除用户
const remove = ({id}) => {
  return UsersModel.deleteOne({_id: id})
}

module.exports = {
  signup,
  find,
  list,
  update,
  remove
}